<?php

namespace maotool\util;

/**
 * 数学工具类
 */
class MathUtil
{

    /**
     * 十分比计算
     * @param $total
     * @param $part
     * @param $precision
     * @param $model
     * @return float|string
     */
    public static function tenPercent($total, $part, $precision = 0, $model = true)
    {
        return self::cutCalculate($total, $part, 10, $precision, $model);
    }

    /**
     * 百分比计算
     * @param $total
     * @param $part
     * @param $precision
     * @param $model
     * @return float|string
     */
    public static function percentage($total, $part, $precision = 0, $model = true)
    {
        return self::cutCalculate($total, $part, 100, $precision, $model);
    }

    /**
     * 千分比计算
     * @param $total
     * @param $part
     * @param $precision
     * @param $model
     * @return float|string
     */
    public static function thousandthRatio($total, $part, $precision = 0, $model = true)
    {
        return self::cutCalculate($total, $part, 1000, $precision, $model);
    }

    /**
     * 自定义分比计算
     * @param $total
     * @param $part
     * @param $cut
     * @param $precision
     * @param $model
     * @return float|string
     */
    public static function cutCalculate($total, $part, $cut = 100, $precision = 0, $model = true)
    {
        if ($model) {
            return round(($part / $total) * $cut, $precision);
        } else {
            return number_format(($part / $total) * $cut, $precision);
        }
    }

}